#include "Part.h"
#include "Common/Timer/TimerAxis.h"

Part::Part()
: m_pMaster(nullptr)
, m_partType(PART_NONE)
{

}

Part::~Part()
{
	g_GetTimerAxis()->KillAllTimer(this);
}

bool Part::Init(Creature *pMaster, uint32_t partType, CharacterDBInfo* pCharacterDB /* = nullptr */)
{
	if (!pMaster) 
		return false; 
	m_pMaster = pMaster; 
	m_partType = partType;
	return true;
}

bool Part::UnInit()
{
	m_pMaster = nullptr;
	m_partType = PART_NONE;
	
	return true;
}

bool Part::ClearState()
{
	g_GetTimerAxis()->KillAllTimer(dynamic_cast<ITimerHandler*>(this));
	g_GetEvent()->UnSubscribeAll(dynamic_cast<IEventExecuteSink*>(this));
	return true;
}

uint32_t Part::getPartType() 
{
	return m_partType;
}

bool Part::SetTimedo(uint32_t timeId, uint64_t iterval, uint32_t callcount)
{
	if (!g_GetTimerAxis()->SetTimer(timeId, iterval, dynamic_cast<ITimerHandler*>(this), callcount))
	{
		LogErrFmtPrint("part settimer fail, partid:%d, timeId:%d, cid:%llu", timeId, m_partType, m_pMaster->GetCid());
		return false;
	}
	return true;
}

void Part::KillTimedo(uint32_t timeId)
{
	g_GetTimerAxis()->KillTimer(timeId, dynamic_cast<ITimerHandler*>(this));
}

void Part::KillAllTimerdo()
{
	killAllTimer(this);
}